<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use think\Db;
use think\Validate;

class MessageController extends HomeBaseController
{
    
    protected $view_share_var = array(
        PRODCUT_CATEGORY_LIST => array( // 产品分类列表 ，对应控制器ProductController,  
            'position' => true,'type'=>3,'isSelect'=>true,'controller'=>'product'
        ), 
        NEWS_CATEGORY_LIST => array(//新闻分类列表,对应控制器NewsController
            'position' => false,'type'=>1,'isSelect'=>false,'controller'=>'news'
        ), 
        CASE_CATEGORY_LIST => array(//案例分类列表，对应控制器CaseController
            'position' => false,'type'=>1,'isSelect'=>false,'controller'=>'case'
        ),  
        PRODCUT_RECOMMEND_LIST => array( // 产品推荐列表 ，对应控制器ProductController,  
            'position' => true,'type'=>2,'isSelect'=>true,'controller'=>'product'
        ),
        NEWS_RECOMMEND_LIST => array( // 新闻推荐列表 ，对应控制器ProductController,  
            'position' => true,'type'=>2,'isSelect'=>true,'controller'=>'news'
        ),
        CASE_RECOMMEND_LIST => array( // 案例推荐列表 ，对应控制器ProductController,  
            'position' => false,'type'=>2,'isSelect'=>false,'controller'=>'case'
        ),        
    );  
    
    public $rules = array(
        'username' => 'require',
        'mobile' => 'require',
       // 'content' => 'require'
    );

    public $msg = array(
        'username.require' => '请输入您的姓名',
        'mobile.require' => '请输入您的联系方式',
       // 'content' => 'require'
    );


    public function index()
    {
        return $this->fetch(':message');
    }
    
    
    
    public function add(){
        $flag = FALSE;
        $message = '留言失败！';
        $request_data = $this->request->param();


        $valiade_obj = new Validate($this->rules,$this->msg);
       
        if($this->request->isPost()){
            if(!$valiade_obj->check($request_data))$message = $valiade_obj->getError ();
            else{
                $add_data = array(
                    'title' => empty($request_data['title'])?'':$request_data['title'],
                    'username' => empty($request_data['username'])?'':$request_data['username'],
                    'mobile' => empty($request_data['mobile'])?'':$request_data['mobile'],
                    'address' => empty($request_data['address'])?'':$request_data['address'],
                    'email' => empty($request_data['email'])?'':$request_data['email'],
                    'content' => empty($request_data['content'])?'':$request_data['content'],
                    'create_time' => time(),                
                );     
                $add_flag = Db::name('message')->insert($add_data);
                if($add_flag){
                    $flag = true;
                    $message = '留言成功！';
                }
            }
        }
        return json(array(
            'code' => $flag ? 1 : 0,
            'str' => $message
        ));
    }
    
}
